package cn.edu.xmu.rocketmqdemo.service;

import cn.edu.xmu.rocketmqdemo.dao.OrdersDao;
import cn.edu.xmu.rocketmqdemo.model.Order;
import cn.edu.xmu.rocketmqdemo.model.OrderVo;
import cn.edu.xmu.rocketmqdemo.model.VoObject;
import cn.edu.xmu.rocketmqdemo.util.ResponseCode;
import cn.edu.xmu.rocketmqdemo.util.ReturnObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrdersService {

    @Autowired
    public OrdersDao ordersDao;

    public ReturnObject<VoObject> findOrderById(Integer id) {
        ReturnObject<Order> returnOrder = ordersDao.findOrderById(id);
        ReturnObject<VoObject> returnVoObject = null;
        if (returnOrder.getCode().equals(ResponseCode.OK)) {
            if (returnOrder.getData().getOrderPo() != null)
                returnVoObject = new ReturnObject<>(returnOrder.getData());
            else
                returnVoObject = new ReturnObject<>(ResponseCode.RESOURCE_ID_NOTEXIST, "资源不存在");
        } else
            returnVoObject = new ReturnObject<>(returnOrder.getCode(), returnOrder.getErrmsg());

        return returnVoObject;
    }

    public ReturnObject<VoObject> createOrder(OrderVo orderVo){
        Order order =orderVo.createOrder();
        ReturnObject<Object> returnOrder = ordersDao.createNewOrder(order);
        ReturnObject<VoObject> returnVo = null;
        if (returnOrder.getCode().equals(ResponseCode.OK))
            returnVo = new ReturnObject<>(ResponseCode.OK);
        else
            returnVo = new ReturnObject<>(returnOrder.getCode(),returnOrder.getErrmsg());

            return returnVo;

    }


}
